/**
 * 
 */
package com.iwords.service.persistence.impl;


import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import com.iwords.constants.Constants;
import com.iwords.service.object.LearningStatusEntity;
import com.iwords.service.persistence.LearningStatusPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-12    
 * @description  LearningStatus-PersistenceImpl        
 */
@Stateless
public class LearningStatusPersistenceImpl implements LearningStatusPersistence {
	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	public void moveSysLib(LearningStatusEntity entity) {
		String sql = "insert into learningstatus(userid,libid,wordid,familiarity,createtime,updatetime) " + "select " + entity.getUserid()
				+ " ,t.libid,t.wordid," + Constants.USER_LEARNINGSTATUS_DEFAULT_VALUE + ", now(),now() from syslibwords t where t.libid=" + entity.getLibid();

		getEntityManager().createNativeQuery(sql).executeUpdate();

	}

	public boolean isExistUserLib(LearningStatusEntity entity) {
		Query query = getEntityManager().createQuery("SELECT count(u) from LearningStatusEntity u where u.userid=?1 and u.libid=?2");
		query.setParameter(1, entity.getUserid());
		query.setParameter(2, entity.getLibid());

		return ((Long) query.getSingleResult()) > 0 ? true : false;

	}

	public void updateLearningStatus(LearningStatusEntity entity) {

		getEntityManager()
				.createQuery("update LearningStatusEntity l set l.familiarity=?1 , l.updatetime= ?2 where l.userid=?3 and l.libid=?4 and l.word.wordid=?5")
				.setParameter(1, entity.getFamiliarity()).setParameter(2, new Date(),TemporalType.TIMESTAMP).setParameter(3, entity.getUserid()).setParameter(4, entity.getLibid())
				.setParameter(5, entity.getWord().getWordid()).executeUpdate();

	}

	@SuppressWarnings("unchecked")
	public List<LearningStatusEntity> getStatusList(LearningStatusEntity entity) {
		Query query = null;
		if (entity.getFamiliarity() == null) {
			query = getEntityManager().createQuery("SELECT u from LearningStatusEntity u where u.userid=?1 and u.libid=?2");
			query.setParameter(1, entity.getUserid());
			query.setParameter(2, entity.getLibid());
		} else {
			query = getEntityManager().createQuery("SELECT u from LearningStatusEntity u where u.userid=?1 and u.libid=?2 and u.familiarity=?3");
			query.setParameter(1, entity.getUserid());
			query.setParameter(2, entity.getLibid());
			query.setParameter(3, entity.getFamiliarity());

		}
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<LearningStatusEntity> getFinalList(LearningStatusEntity entity) {
		Query query = null;
		if (entity.getLibid() == null) {
			query = getEntityManager().createQuery("SELECT u FROM LearningStatusEntity u WHERE u.familiarity=?1 and u.userid=?2");
			query.setParameter(1, entity.getFamiliarity());
			query.setParameter(2, entity.getUserid());
		} else {
			query = getEntityManager().createQuery("SELECT u FROM LearningStatusEntity u WHERE u.familiarity=?1 and u.userid=?2 and u.libid=?3");
			query.setParameter(1, entity.getFamiliarity());
			query.setParameter(2, entity.getUserid());
			query.setParameter(3, entity.getLibid());
		}
		return query.getResultList();
	}

	 

	public long getStudyLibNum(Long userid, Long syslibid, int level) {
		Query query = null;
		if (syslibid == null) {
			query = getEntityManager().createQuery("SELECT count(u) from LearningStatusEntity u where u.userid=?1  and u.familiarity=?2");
			query.setParameter(1, userid);
			query.setParameter(2, level);
		} else {

			query = getEntityManager().createQuery("SELECT count(u) from LearningStatusEntity u where u.userid=?1 and u.libid=?2 and u.familiarity=?3");
			query.setParameter(1, userid);
			query.setParameter(2, syslibid);
			query.setParameter(3, level);
		}
		try {
			return ((Long) query.getSingleResult());

		} catch (NoResultException e) {
			return 0;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Long> getAllStudyList(Long userid, int toPage) {
		Query query = getEntityManager().createQuery("SELECT DISTINCT u.libid FROM LearningStatusEntity u  where u.userid=?1")
				.setFirstResult((toPage - 1) * 10).setMaxResults(10);
		query.setParameter(1, userid);
		return (List<Long>) query.getResultList();
	}

	public int getAllStudyListNum(Long userid) {
		Query query = getEntityManager().createQuery("SELECT count(DISTINCT u.libid) FROM LearningStatusEntity u  where u.userid=?1");
		query.setParameter(1, userid);

		int tp = 0;
		long r = (Long) query.getSingleResult();

		if (r % 10 == 0) {
			tp = (int) (r / 10);
		} else {
			tp = (int) (r / 10) + 1;
		}

		return tp;
	}

	 
	public LearningStatusEntity getLearningStatusWord(LearningStatusEntity entity) {
		Query query = getEntityManager().createQuery("SELECT u from LearningStatusEntity u where u.userid=?1 and u.libid=?2 and u.word.wordid=?3");
		query.setParameter(1, entity.getUserid());
		query.setParameter(2, entity.getLibid());
		query.setParameter(3, entity.getWord().getWordid());

		return (LearningStatusEntity) query.getSingleResult();
	}

}
